# -*- coding: utf-8 -*-
#easy_install mechanize
import mechanize
import cookielib
import time
from bs4 import BeautifulSoup,Tag
from urllib import myquote_plus
from langconv import *
import random
from urllib import myquote_plus
# Browser

__all__ = ['InitBrowser','brgoogle_relation_confidence']

__br = None
 
def InitBrowser(proxies= True):
    global __br
    if __br:
        __br.close()
        
    __br = mechanize.Browser()
    # Cookie Jar
    cj = cookielib.LWPCookieJar()
    __br.set_cookiejar(cj)
    
    # Browser options
    __br.set_handle_equiv(True)
    __br.set_handle_gzip(True)
    __br.set_handle_redirect(True)
    __br.set_handle_referer(True)
    __br.set_handle_robots(False)
    
    # Follows refresh 0 but not hangs on refresh > 0
    __br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
    # Want debugging messages?
    #__br.set_debug_http(True)
    #__br.set_debug_redirects(True)
    #__br.set_debug_responses(True)
    
    # User-Agent (this is cheating, ok?)
    __br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
    if proxies:
        __br.set_proxies({"http": "127.0.0.1:8087"})
    
#现在已经创建好了__br这个浏览器对象，可以用它来打开页面，或者进行交互。
#



def brgoogle_relation_confidence(key1_name,key2_name,key12_ral='',num=10,pause=2):
    url_search = "http://www.google.%(tld)s/search?hl=%(lang)s&q=%(query)s&btnG=Google+Search"
    tld='com.tw'
    lang='en'
    query = (key1_name+u' '+key12_ral+u' '+key2_name)
    query = myquote_plus(query, encoding='utf-8')
    url = url_search % vars()
 
    sleep_time  = max([5.0*random.randint(0, 100)/100.0,2])
    print '@\t begin waiting',(key1_name+u' '+key12_ral+u' '+key2_name),sleep_time
    time.sleep(sleep_time)
    print '@\t end waiting',(key1_name+u' '+key12_ral+u' '+key2_name),sleep_time
    
    global __br
    r = __br.open(url)
    print '@\t',__br.title()
    html = r.read()
    
    
    
    results = []
    link_set = []
    cur_page=0
    while True:
        soup = BeautifulSoup(html)            
        #soup_body = soup.find('body')
        
        
        glst = soup.find_all('li',{'class':'g'})
        for itm in glst:
            head_str = ''
            body_str = ''
            for cld in itm.children:
                if cld.name.strip() == 'a':
                    head_str = Converter('zh-hans').convert(cld.text)
                else:
                    body_str = body_str + '\n' + Converter('zh-hans').convert(cld.text)
            results.append( [head_str,body_str] )
            
        '''
        res_pair=None
        for cld in soup_body.children:
            if type(cld) == Tag:
                if cld.name.strip() == 'p':
                    if res_pair:
                        results.append(res_pair)
                    res_pair=[ Converter('zh-hans').convert(cld.text), '' ]
                elif res_pair and cld.name.strip() == 'table':
                    res_pair[1] = Converter('zh-hans').convert(cld.text)
        if res_pair:
            results.append(res_pair)
        '''
        
        if not link_set:
            for link in __br.links():
                if link.base_url.find('/search?')!=-1 and link.url.find('start=')!=-1 :
                    link_set.append(link)
                    
        if len(results)<num and cur_page<11:
            cur_page = cur_page +1
            print '@\tget ',cur_page,'page'
            time.sleep(pause)
            r = __br.follow_link(link_set[cur_page-1])
            html = r.read()
            #print 'finish ',cur_page,'page' 
        else:
            break  
    
    fcnt = 0
    all_w = 0
    fidx = 0
    for res in results:
        fidx = fidx+1.0
        key1_found = ( res[0].find(key1_name)!=-1 ) or ( res[1].find(key1_name)!=-1 )
        key2_found = ( res[0].find(key2_name)!=-1 ) or ( res[1].find(key2_name)!=-1 )
        ral_found  = ( res[0].find(key12_ral)!=-1 ) or ( res[1].find(key12_ral)!=-1 ) or key12_ral==''
        all_w = all_w + 1.0/fidx
        if key1_found and key2_found and ral_found:
            fcnt = fcnt + 1.0/fidx
    
    return (fcnt/all_w ,  results) 

    


    
